﻿using MaterialDesignThemes.Wpf;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TestingPlatForm.MissionContrl.ViewModels;

namespace TestingPlatForm.MissionContrl
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            btnMin.Click += (s, e) => { this.WindowState = WindowState.Minimized; };
            btnMax.Click += (s, e) =>
            {
                if (this.WindowState == WindowState.Maximized)
                    this.WindowState = WindowState.Normal;
                else
                    this.WindowState = WindowState.Maximized;
            };
            btnClose.Click += async (s, e) =>
            {
                this.Close();
            };

            MouseMove += (s, e) =>
                {
                    var position = e.GetPosition(this);
                    if (e.LeftButton == MouseButtonState.Pressed && RestrictedArea.InputHitTest(position) != null)
                        this.DragMove();
                };

            MouseDoubleClick += (s, e) =>
            {
                var position = e.GetPosition(this);
                if (this.WindowState == WindowState.Normal && RestrictedArea.InputHitTest(position) != null)
                    this.WindowState = WindowState.Maximized;
                else
                    this.WindowState = WindowState.Normal;
            };
        }

        private void MenuToggleButton_OnClick(object sender, RoutedEventArgs e)
        {
            if (ActualWidth > 1600)
            {
                NavRail.Visibility = Visibility.Collapsed;
                MenuToggleButton.Visibility = Visibility.Collapsed;
            }

        }

        private void MenuDarkModeButton_Click(object sender, RoutedEventArgs e)
            => ModifyTheme(DarkModeToggleButton.IsChecked == true);

        private void FlowDirectionButton_Click(object sender, RoutedEventArgs e)
            => FlowDirection = FlowDirectionToggleButton.IsChecked.GetValueOrDefault(false)
                ? FlowDirection.RightToLeft
                : FlowDirection.LeftToRight;

        private static void ModifyTheme(bool isDarkTheme)
        {
            var paletteHelper = new PaletteHelper();
            var theme = paletteHelper.GetTheme();

            theme.SetBaseTheme(isDarkTheme ? BaseTheme.Dark : BaseTheme.Light);
            paletteHelper.SetTheme(theme);
        }

        //private void OnSelectedItemChanged(object sender, DependencyPropertyChangedEventArgs e)
        //    => MainScrollViewer.ScrollToHome();

        private void MenuOpen_Click(object sender, RoutedEventArgs e)
        {
            NavDrawer.IsLeftDrawerOpen = false;
            if (ActualWidth > 1600)
            {
                NavRail.Visibility = Visibility.Visible;
                MenuToggleButton.Visibility = Visibility.Visible;
            }

        }
        //private void UIElement_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        //{
        //    if (NavDrawer.OpenMode is not DrawerHostOpenMode.Standard)
        //    {
        //        //until we had a StaysOpen flag to Drawer, this will help with scroll bars
        //        var dependencyObject = Mouse.Captured as DependencyObject;

        //        while (dependencyObject != null)
        //        {
        //            if (dependencyObject is ScrollBar) return;
        //            dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
        //        }

        //        MenuToggleButton.IsChecked = false;
        //    }
        //}

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (ActualWidth <= 700)
            {
                NavDrawer.OpenMode = DrawerHostOpenMode.Modal;
                NavDrawer.IsLeftDrawerOpen = false;
                MenuToggleButton.Visibility = Visibility.Visible;
            }
            else if (ActualWidth > 700 && ActualWidth <= 1600)
            {
                NavDrawer.OpenMode = DrawerHostOpenMode.Modal;
                NavDrawer.IsLeftDrawerOpen = false;
                MenuToggleButton.Visibility = Visibility.Visible;
            }
            else if (ActualWidth > 1600)
            {
                NavDrawer.OpenMode = DrawerHostOpenMode.Standard;
                NavDrawer.IsLeftDrawerOpen = true;
                MenuToggleButton.Visibility = Visibility.Collapsed;
            }
        }
    }
}